package yl.hs.bmipfull.controllers.report;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.production.IProductionRecordService;
import yl.hs.bmipfull.service.report.ICostingService;
import yl.hs.bmipfull.service.report.IProductionStatisticsService;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 作用:   <br/>
 * 创建时间: 2022-02-23 14:38 <br/>
 * 生产统计web接口
 *
 * @author 吴高丰
 */
@Api(value = "生产统计web接口", tags = "报表中心-生产统计")
@RestController
@RequestMapping(value = "/api/productionstatistics")
public class ProductionStatisticsController extends ControllerBase {
    @Autowired
    IProductionStatisticsService statisticsService;
    @Autowired
    IProductionRecordService recordService;
    @Autowired
    ICostingService costingService;

    @RequestMapping(value = "/Commercialconcrete/{productCateSn}", method = RequestMethod.POST)
    public WebResult vehicleDeliveryStatistics(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery, @ApiParam(value = "产品编号") @PathVariable(required = false) String productCateSn) {
        return success("查询成功", statisticsService.vehicleDeliveryStatistics(pageQuery, productCateSn));
    }

    @RequestMapping(value = "/production_sales_comparison/{productCateSn}", method = RequestMethod.POST)
    public WebResult queryProductionSalesComparison(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery, @ApiParam(value = "产品编号") @PathVariable(required = false) String productCateSn) {
        try {
            return success("查询成功", statisticsService.queryProductionSalesComparison(pageQuery, productCateSn));
        } catch (Exception e) {
            return error(e);
        }
    }

    @RequestMapping(value = "/ProductConsumption/{productCateSn}", method = RequestMethod.POST)
    public WebResult queryProductConsumption(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery, @ApiParam(value = "产品编号") @PathVariable(required = false) String productCateSn) throws Exception {
        return success("查询成功", statisticsService.queryProductConsumption(pageQuery, productCateSn));
    }

    @RequestMapping(value = "/ProductLineConsumption", method = RequestMethod.POST)
    public WebResult queryProductLineConsumption(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) throws Exception {
        return success("查询成功", statisticsService.queryProductLineConsumption(pageQuery, ""));
    }

    @RequestMapping(value = {"/SaleAndProductionGroupDay/{startTime}/{endTime}", "/SaleAndProductionGroupDay/{startTime}/{endTime}/{productLineSN}"}, method = RequestMethod.POST)
    public WebResult querySaleAndProductionGroupDay(@ApiParam(value = "开始时间") @PathVariable(required = false) String startTime,
                                                    @ApiParam(value = "结束时间") @PathVariable(required = false) String endTime,
                                                    @ApiParam(value = "生产线编号") @PathVariable(required = false) String productLineSN) throws ParseException {
        return success("查询成功", statisticsService.querySaleAndProductionGroupDay(startTime, endTime, productLineSN));
    }

    @ApiOperation(value = "查询生产消耗成本分析信息", notes = "生产消耗成本分析信息")
    @PostMapping("/record/cost/{productCateSn}")
    public WebResult queryProductionRecordCost(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery, @ApiParam(value = "产品编号") @PathVariable String productCateSn) {
        return success("ok", statisticsService.queryProductionRecordCost(pageQuery, productCateSn));
    }

    @ApiOperation(value = "查询工程生产消耗成本分析信息", notes = "工程生产消耗成本分析信息")
    @PostMapping("/project/cost/{productCateSn}")
    public WebResult queryProjectProductionCost(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery, @ApiParam(value = "产品编号") @PathVariable String productCateSn) {
        return success("ok", statisticsService.queryProjectProductionCost(pageQuery, productCateSn));
    }

    @ApiOperation(value = "查询生产消耗成本分析信息", notes = "生产消耗成本分析信息")
    @PostMapping("/record/mortar/{startTime}/{endTime}")
    public WebResult mortarProductionRecord(@ApiParam(value = "开始时间") @PathVariable(required = false) String startTime, @ApiParam(value = "开始时间") @PathVariable(required = false) String endTime) {
        return success("ok", statisticsService.mortarProductionRecord(startTime, endTime));
    }

    @ApiOperation(value = "工程发货统计信息", notes = "工程发货统计信息")
    @PutMapping("/project/summary/{cateSN}/{startTime}/{endTime}")
    public WebResult queryProjectSummary(@PathVariable String cateSN, @PathVariable String startTime, @PathVariable String endTime) {
        try {
            return success("ok", statisticsService.queryProjectShipSummary(cateSN, Tl.strToDate(startTime), Tl.strToDate(endTime)));
        } catch (Exception ex) {
            return error(ex);
        }
    }

    @ApiOperation(value = "工程发货统计信息", notes = "工程发货统计信息")
    @PutMapping("/project/day/{cateSN}/{projectSN}/{startTime}/{endTime}")
    public WebResult queryProjectDayProductSummary(@PathVariable String cateSN, @PathVariable String projectSN, @PathVariable String startTime, @PathVariable String endTime) {
        try {
            return success("ok", statisticsService.queryProjectDaySummary(cateSN, projectSN, Tl.strToDate(startTime), Tl.strToDate(endTime)));
        } catch (Exception ex) {
            return error(ex);
        }
    }

    @ApiOperation(value = "生产板块管理看板", notes = "生产板块管理看板")
    @GetMapping("/productionBoard/{year}/{month}")
    public WebResult productionBoard(@PathVariable("year") Integer year, @PathVariable("month") Integer month) {
        return success("查询成功", statisticsService.getProductionBoardData(year, month));
    }

    @ApiOperation(value = "每日消耗明细表", notes = "每日消耗明细表")
    @GetMapping({"/costing/{date}/{unit}","/costing/{date}"})
    public WebResult productionBoard(@PathVariable("date") String date, @PathVariable(value="unit",required=false) String unit) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date dates = sdf.parse(date);
            return success("查询成功", costingService.queryList(dates, unit));
        }catch (Exception ee){
            return error(ee);
        }
    }
}
